

*****************************************
*****************************************
****Presidential Campaigns and the Fundamentals Reconsidered
****Peter K. Enns and Brian Richman
****Journal of Politics
****Replication: CA Treatment Effect
*****************************************
*****************************************


***Set Working Directory"
cd ""

*****************
*****SET UP******
***************** ;


*Change delimiter to be ";" ;
	#d ;

*Clear all existing data ;
	clear ;

*Set more to off ;
	set more off ;

*Install coarsened matching program ;
	/* Matthew Blackwell, Stefano M. Iacus, Gary King, and Giuseppe Porro (2009), "cem: Coarsened exact matching in Stata", The Stata Journal, 
		9(4), 524-546, copy at <http://gking.harvard.edu/files/cemStata.pdf> */ ;
*Only needs to be installed once ;
*To disable, add "*" in front of lines ;
	*ssc install cem ;

	set mem 1000000k ;

use "EnnsRichman_NAES_VoteIntentions.dta", clear ;

************************************************************** ;
**Change delimeter to be end of line ;
#d cr
**************************************************************

************************************************************************************************************
**Create time windows variable
************************************************************************************************************

gen windows = .
	replace windows = -1 if days_elec <= -1 & days_elec >= -21
	replace windows = -2 if days_elec <= -22 & days_elec >= -39
	replace windows = -3 if days_elec <= -40 & days_elec >= -60
	replace windows = -4 if days_elec <= -61 & days_elec >= -80
	replace windows = -5 if days_elec <= -81 & days_elec >= -100
************************************************************************************************************


 	
*******************************************************************
**Create dummy variable for each state 

foreach x in AL	AR	AZ	CA	CO	CT	DC	DE	FL	GA	IA	ID	IL	IN	KS	KY	LA	MA	MD	ME	MI	MN	MO	MS	MT	NC	ND	NE	NH	NJ	NM	NV	NY	OH	OK	OR	PA	RI	SC	SD	TN	TX	UT	VA	VT	WA	WI	WV	WY {

gen `x' = (cst == "`x'")
}
*******************************************************************


************************
*****SETUP ANALYSIS*****
************************ ;



*********************************************************************
**Keep variables that are needed for analysis
keep registered CA zeduc zexp10 zknow18 windows correct_pv15 cst ztot_presad_exp3 noaddata
**********************************************************************



***************************************************************************
**Create temporary datafile to use for analysis named "match_setup"
**"match_setup" has all of data setup and will be called to begin each calculation below

*Declare name of tempfile
tempfile match_setup

*Save tempfile
save `match_setup', replace
******************************************************************************



*******************************************************************************
**Create dataset where results will be stored (using "postfile" commands)

**Closes postfile dataset if dataset is already open
capture postclose results


**Creates dataset using postfile command
postfile results windows str2(state) estimate se matched unmatched treated using "CA_Analysis.dta", replace
*********************************************************************************










******************************************
*****COARSENED EXACT MATCHING ANALYSIS*****
******************************************* ;

*********************************************************************************
**Analysis for CA
***Loop by window (e.g., window 1 = days -1 to -20)
forval i = 1/5 {



	**Conduct calculation for CA
	foreach x in CA {


		**Clear all data
		clear
	
		**Use tempfile with all data setup
		use `match_setup'

		**Keep only registered voters
		keep if registered == 1

		**Use only one time window at a time
		keep if windows == -`i'
	
		**Conduct coarsened match (See "help cem" for more details on program)
		**Description of coarsening:
			**  #0 = do not coarsen.  Exact values matched.
			**  #n = divide variable into n equally spaced cutpoints

			cem zknow18 zeduc zexp10 ztot_presad_exp3 noaddata(#0), treatment(`x')			


		**Collect statistics on number of observations matched, unmatched, and treated 
			**Cross tabulation of treatment and matched status (as matrix)
			matrix A = r(match_table)

			**Read data from cross tab matrix
			local matched = r(n_matched)
			local unmatched = A[3,1]
			local treated = A[1,2]
		


		
	
	
		**Estimate the sample average treatment effect on the treated (SATT) 
		** Because some variables are coarsened, some imbalance will remain.
		** We therefore include control variables in the regression model to control for remaining imbalance (Blackwell, Iacus, King, and Porro 2009, p.537)
		** "new_weight" = the weight assigned to the observation's stratum. Equals 0 if the observation is unmatched and 1 if 
			**the observation is treated.

 		xi: logit correct_pv15 `x' zeduc zknow18 zexp10 ztot_presad_exp3 noaddata [iweight=cem_weights]
					

		**Add results data to dataset created using postfile
		post results (-`i') ("`x'") (_b[`x']) (_se[`x']) (`matched') (`unmatched') (`treated') 
	
		**Clear all data
		clear


	**End loop for CA
	}
	

**End loop by window
}
*********************************************************************************




 ********************************************
**Close postfile dataset
postclose results
********************************************







*********************************
*****SETUP TO GRAPH RESULTS*****
********************************* ;
 *************************************************************
**Open results dataset
use "CA_Analysis.dta"
**************************************************************




********************************************************
**Sort by state and time window
sort state windows
*********************************************************



**************************************************************************
**Create hi and low levels of 95% C.I. for estimates for CA
	gen hi = estimate + 1.96*se
	gen low = estimate - 1.96*se
***************************************************************************




****************************************************
**Set color scheme for graphs

set scheme s1mono
****************************************************

***********************
*****GRAPH RESULTS*****
*********************** ;

*********************************************
***Create graph for CA only 
twoway rcap hi low windows if windows>=-5 &  state == "CA", lcolor(black) || line estimate windows if state == "CA" & windows>=-5, lcolor(black) yline(0, lstyle(foreground)) legend(off) xlabel(-.5 "0" -1.5 "-20" -2.5 "-40" -3.5 "-60" -4.5 "-80" -5.5 "-100") xtitle("Days before election") ytitle("Coefficient and 95% Confidence Interval for CA")
*********************************************



*************
*************
*************
*****END*****
*************
*************
************* 


